package ar.org.cfired.creditos.guias;

// Import the servlet classes

import javax.servlet.*;
import javax.servlet.http.*;

// Import the standard Java classes
import java.io.*;
import java.util.*;

//import ar.org.cfired.creditos.guias.*;
import ar.org.cfired.hibernate.utils.HibernateUtil;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class IndiceGuia extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public void init() throws ServletException {
	    // Initialization here...
	  }

	  public void service( HttpServletRequest req, HttpServletResponse res ) 
	        throws ServletException 
	  {
    try
    {
        String cre_id = req.getParameter( "cre_id" );
        String gui_id = new String();
        if( cre_id == null ) {
          cre_id = "139";
        }
        String impresion = req.getParameter("impresion");
    	Object[] r;
    	
    	// Write the response
      	 res.setContentType("text/html; charset=iso-8859-1"); 
     PrintWriter out = res.getWriter();

     Transaction t;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		t = session.beginTransaction();
		
		
		/* Encabezado htm */
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\r\n   \"http://www.w3.org/TR/html4/loose.dtd\">");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\" >");
		out.println("<script src=\"/js/guias/ajaxPreGuardadoGuias.js\" language=\"Javascript\" type=\"text/javascript\"></script>");
		out.println("<link REL=\"STYLESHEET\" HREF=\"/css/guias/guias.css\" type=\"text/css\">");
		out.println("</head>");
		out.println("<body>");
		out.println("<input type=\"hidden\" id=\"cre_id\" name=\"cre_id\" value=\"" + cre_id + "\">\n");
		
		/* Leo el indice */
		List res3 = session.createSQLQuery("execute leer_guia_indice @cre_id=" + cre_id ).list();
		Iterator iter3 = res3.iterator();
		int j = 0;
		
		Vector<String> v = new Vector();
		String actual = new String(); 
		while (iter3.hasNext()) {
			r = (Object[])iter3.next();
			ElementoIndice resG = new ElementoIndice();
			resG.parseObject(r);
			resG.setCreId(new Integer(cre_id));
			if (!v.isEmpty()) {
				if (resG.getNumero().contains(actual)) {
					out.println("<ul>");
					v.add(resG.getNumero().substring(0,((resG.getNumero().lastIndexOf(".")>0)?(resG.getNumero().lastIndexOf(".")):(resG.getNumero().length()))));
				}
				else if (resG.getNumero().lastIndexOf(".") < actual.lastIndexOf(".")) {
					while (resG.getNumero().lastIndexOf(".") < actual.lastIndexOf(".")) {
						out.println("</ul>");
						v.remove(v.size()-1);
						actual = v.get(v.size()-1);
					}
					//out.println("<ul>");
					
				}
			}
			else {
				out.println("<ul>");
				v.add(resG.getNumero());
			}
			if (impresion != null) out.print(resG.toHtmlImp());
			else out.print(resG.toHtml());
			actual = resG.getNumero();
			j++;
			
			//out.println(resG.toHtmlImp());
			/* PostComentario */
		}
		//out.println("</ul>");
		out.println("</body></html>");		
     

    }
    catch( IOException ioe )
    {
     throw new ServletException( ioe );
    }
  }
  public void destroy() {
    // Cleanup here...
  }

}
